/**
 * @Author 韦建韩
 * @Date 2022/3/24
 */
package com.example.devops.web.controller.applicationAdmin;

import com.example.devops.constant.HttpCodeConstant;
import com.example.devops.entity.common.Result;
import com.example.devops.exception.CustomException;
import com.example.devops.exception.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionController {
    Logger logger = LoggerFactory.getLogger(ExceptionController.class);
    @ExceptionHandler({CustomException.class})
    @ResponseBody
    public Result defaultExceptionHandler(CustomException e){
        logger.error("error:",e);
        Result result = new Result();
        result.setCode(e.getCode());
        result.setData(e.getMessage());
        return result;
    }
    @ExceptionHandler({ValidationException.class})
    @ResponseBody
    public Result validationExceptionHandler(Exception e){
        logger.error("error:",e);
        Result result = Result.doCreate();
        result.setCode(HttpCodeConstant.Field.AuthenticationError.code);
        result.setData(e.getMessage());
        return result;
    }
    @ExceptionHandler({Exception.class})
    @ResponseBody
    public Result SystemExceptionHandler(Exception e){
        logger.error("error:",e);
        Result result = Result.doCreate();
        result.setCode(HttpCodeConstant.Field.ServerError.code);
        result.setData("系统错误，请联系管理员");
        return result;
    }
}
